﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Data.SqlClient;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;
using System.IO;
using System.Text.RegularExpressions;


namespace Service.Presentation.WinForm
{
    public partial class frm_KhachHang : Telerik.WinControls.UI.RadForm
    {
        static frm_KhachHang doituong = null;
        public static frm_KhachHang TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frm_KhachHang();
            return doituong;
        }
        public frm_KhachHang()
        {
            InitializeComponent();
        }
        //Khai bao cac bien
        //Bien danh sach khach hang
        DataSet dsKH;// = new DataSet();
        //Bien WebService Xu Ly Khach Hang
        QuanLyNhapXuat.RS_XuLiKhachHang.WS_KhachHangSoapClient xlkh = new QuanLyNhapXuat.RS_XuLiKhachHang.WS_KhachHangSoapClient();

        public int MaKH_MoiThem;
        private void frm_KhachHang_Load(object sender, EventArgs e)
        {
            //Lay Danh Sach Khach Hang Do Len mcbb_TenKH
            LoadKhachHang();
            //Tao khach hang ke tiep
            btn_LuuMoi.Text = "Lưu";
            TaoKHKeTiep();
            btn_CapNhat.Enabled = false;
            
        }
        private void CapNhatKH()
        {
            int makh = int.Parse(txt_MaKH.Text);
            string hokh = txt_HoKH.Text;
            string tenkh = mcbb_tenkh.Text; ;
            bool gioitinh = rdb_Nu.Checked;
            string sdt = mask_txt_SoDT.Text; ;
            string diachi = txt_DiaChi.Text;
            try
            {
                xlkh.sua(makh, hokh, tenkh, gioitinh, diachi, sdt);
                //LoadKhachHang();
            }
            catch (SqlException sqlex)
            {
                //throw sqlex;
            }
            catch (ApplicationException appex)
            {
                //throw appex;
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
        private void TaoKHKeTiep()
        {
            try
            {
                DataSet dsTop = new DataSet();
                dsTop.ReadXml(new StringReader(xlkh.selectquery("select top 1 makh from KHACHHANG order by makh desc")));
                DataTable bang = dsTop.Tables[0];
                int makh_ketiep = 0;
                DataTableReader dtbr = bang.CreateDataReader();
                while (dtbr.Read())
                {
                    makh_ketiep = int.Parse(dtbr[0].ToString()) + 1;
                    txt_MaKH.Text = makh_ketiep.ToString();
                }
                btn_LuuMoi.Text = "Lưu";
                btn_CapNhat.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
        private void SuaKH()
        {
            if (mcbb_tenkh.SelectedIndex > -1 && mcbb_tenkh.Text.Trim() != "")
            {
                DataSet dsSua = new DataSet();
                dsSua.ReadXml(new StringReader(xlkh.selectquery("select makh,hokh,gioitinh,sdt,diachi from khachhang where makh= '" + mcbb_tenkh.SelectedValue + "'")));
                DataTable bang = dsSua.Tables[0];
                DataTableReader dtbr = bang.CreateDataReader();                
                while (dtbr.Read())
                {
                    txt_MaKH.Text = dtbr[0].ToString();
                    txt_HoKH.Text = dtbr[1].ToString();
                    rdb_Nam.Checked = bool.Parse(dtbr[2].ToString());
                    mask_txt_SoDT.Text = dtbr[3].ToString();
                    txt_DiaChi.Text = dtbr[4].ToString();
                }
                
            }
            
        }
        private void LoadKhachHang()
        {
            try
            {
                //Chinh do dai cua cac cot trong muiltcombobox
                RadMultiColumnComboBoxElement multiColumnComboElement = this.mcbb_tenkh.MultiColumnComboBoxElement;
                multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
                multiColumnComboElement.DropDownMinSize = new Size(300, 200);

                dsKH = new DataSet();
                dsKH.ReadXml(new StringReader(xlkh.layds()));
                mcbb_tenkh.DataSource = dsKH.Tables[0];
                mcbb_tenkh.DisplayMember = "tenkh";
                mcbb_tenkh.ValueMember = "makh";
                mcbb_tenkh.SelectedIndex = -1;

                FilterDescriptor des_sn = new FilterDescriptor(this.mcbb_tenkh.DisplayMember, FilterOperator.StartsWith, string.Empty);
                mcbb_tenkh.EditorControl.FilterDescriptors.Add(des_sn);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi" + ex.Message);
            }
            mcbb_tenkh.Text = null;
            txt_HoKH.Text = "";
            txt_DiaChi.Text = "";
            mask_txt_SoDT.Text = "";
            
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            string hokh = txt_HoKH.Text;
            string tenkh = mcbb_tenkh.Text;
            bool gioitinh = rdb_Nu.Checked;
            string sdt = mask_txt_SoDT.Text;
            string diachi = txt_DiaChi.Text;
            string tentaikhoan = tenkh + hokh;
            string matkhau = "12345678";
            if (btn_LuuMoi.Text.Trim() == "Lưu")
            {
                if (txt_HoKH.Text.Trim() == "")
                {
                    MessageBox.Show("Chưa nhập họ khách hàng!");
                    return;
                }
                if (mcbb_tenkh.Text.Trim() == "")
                {
                    MessageBox.Show("Chưa nhập tên khách hàng!");
                    return;
                }
                if (mask_txt_SoDT.Text.Trim() == "")
                {
                    MessageBox.Show("Chưa nhập số điện thoại!");
                    return;
                }
                if (txt_DiaChi.Text.Trim() == "")
                {
                    MessageBox.Show("Chưa nhập địa chỉ!");
                    return;
                }
                else
                {
                    try
                    {
                        xlkh.them(hokh, tenkh, gioitinh, diachi, sdt, tentaikhoan, matkhau);
                        
                    }
                    catch (SqlException sqlex)
                    {
                        MessageBox.Show("Lỗi kết nối: " + sqlex.Message);
                    }
                    catch (ApplicationException appex)
                    {
                        MessageBox.Show("Lỗi ứng dụng: " + appex.Message);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Lỗi chương trình:" + ex.Message);
                    }

                    txt_HoKH.Clear();
                    txt_DiaChi.Clear();
                    mask_txt_SoDT.Clear();
                    mcbb_tenkh.Text = null;
                    txt_MaKH.Clear();
                    MessageBox.Show("Đã thêm thành công!");
                    LoadKhachHang();
                    TaoKHKeTiep();
                    
                    //MaKH_MoiThem = int.Parse(txt_MaKH.Text);
                    
                }
            }
            else if (btn_LuuMoi.Text.Trim() == "Thêm mới")
            {
                txt_HoKH.Clear();
                txt_DiaChi.Clear();
                mask_txt_SoDT.Clear();
                mcbb_tenkh.Text = null;
                txt_MaKH.Clear();
                TaoKHKeTiep();
                btn_LuuMoi.Text = "Lưu";
                btn_CapNhat.Enabled = false;
            }
        }

        private void btn_Dong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mcbb_tenkh_SelectedIndexChanged(object sender, EventArgs e)
        {                        
            SuaKH();
            btn_LuuMoi.Text = "Thêm mới";
            btn_CapNhat.Enabled = true;
        }

        private void btn_XacNhan_Click(object sender, EventArgs e)
        {
            CapNhatKH();
            MessageBox.Show("Thanh Cong");
            LoadKhachHang();
            TaoKHKeTiep();
        }

        private void rdb_Nam_CheckedChanged(object sender, EventArgs e)
        {
            rdb_Nu.Checked = !rdb_Nam.Checked;
        }

        private void mcbb_tenkh_Leave(object sender, EventArgs e)
        {
            if (mcbb_tenkh.Text.Trim() != "")
            {
                string patern = @"^[^0-9]\w[^0-9]+$";
                Regex rg = new Regex(patern);
                Match m = rg.Match(mcbb_tenkh.Text);
                if (!m.Success)
                {
                    MessageBox.Show("Tên chỉ được nhập chữ !!!");
                    mcbb_tenkh.Focus();
                }
            }
        }

        private void txt_HoKH_Leave(object sender, EventArgs e)
        {
            if (txt_HoKH.Text.Trim() != "")
            {
                string patern = @"^[^0-9]\w[^0-9]+$";
                Regex rg = new Regex(patern);
                Match m = rg.Match(txt_HoKH.Text);
                if (!m.Success)
                {
                    MessageBox.Show("Họ chỉ được nhập chữ !!!");
                    txt_HoKH.Focus();
                }
            }
        }

        private void mask_txt_SoDT_Leave(object sender, EventArgs e)
        {
            if (mask_txt_SoDT.Text.Trim() != "")
            {
                string patern = @"^\d{8,11}$";
                Regex rg = new Regex(patern);
                Match m = rg.Match(mask_txt_SoDT.Text);
                if (!m.Success)
                {
                    MessageBox.Show("Số điện thoại chỉ được từ 8 tới 11 số!!!");
                    mask_txt_SoDT.Focus();
                }
            }
        }

        private void mask_txt_SoDT_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
          
        }
    }
}
